package com.sbs.gaoxinqutoilet.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.sbs.gaoxinqutoilet.R;
import com.sbs.gaoxinqutoilet.entity.HomeMenuEntity;

import java.util.List;

/**
 * Created by 聪明一只哈 on 2019/10/29.
 * Time: 2019/10/29  10:52
 */
public class SeckillLayoutAdapter extends DelegateAdapter.Adapter{

    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private List<HomeMenuEntity> mItemBeanList;
    private int mCount = 1;

    public SeckillLayoutAdapter(Context mContext, LayoutHelper mLayoutHelper, List<HomeMenuEntity> mItemBeanList) {
        this.mContext = mContext;
        this.mLayoutHelper = mLayoutHelper;
        this.mItemBeanList = mItemBeanList;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new HorizontalLayoutViewHolder(LayoutInflater.from(mContext).inflate(R.layout.vlayout_item_horizontal,viewGroup,false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        HorizontalLayoutViewHolder horizontalListViewHolder= (HorizontalLayoutViewHolder) viewHolder;

        bindHorizontalList(horizontalListViewHolder);
    }

    private void bindHorizontalList(HorizontalLayoutViewHolder holder) {

        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        holder.recyclerView.setLayoutManager(layoutManager);

        SeckillListViewAdapter listViewAdapter = new SeckillListViewAdapter(mContext, mItemBeanList);
        holder.recyclerView.setAdapter(listViewAdapter);

        listViewAdapter.setOnHorizontalListItemClickLitener(new SeckillListViewAdapter.OnHorizontalListItemClickLitener() {
            @Override
            public void onItemClick(HomeMenuEntity itemBean) {
                if(horizontalListCallback!=null){
                    horizontalListCallback.clickHorizontalItem(itemBean);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mCount;
    }

    static class HorizontalLayoutViewHolder extends RecyclerView.ViewHolder{

        private RecyclerView recyclerView;

        public HorizontalLayoutViewHolder(View itemView) {
            super(itemView);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.rv_horizontal);
        }
    }

    /**系列片点击回调*/
    public interface HorizontalListCallback{
        void clickHorizontalItem(HomeMenuEntity itemBean);
    }

    /**系列片点击回调*/
    private HorizontalListCallback horizontalListCallback;

    public void setHorizontalListCallback(HorizontalListCallback horizontalListCallback){
        this.horizontalListCallback = horizontalListCallback;
    }
}
